package com.ljf.common.utils;

import org.springframework.util.StringUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class ParamsUtil {
    //从map的params从取一个List<Integer>
    public static List<Integer> getIntegerList(Map<String, Object> params, String field) {
        if (!params.containsKey(field)) {
            return List.of();
        }
        String tmp = (String) params.get(field);
        List<Integer> list = new LinkedList<>();
        for (String s : tmp.split(",")) {
            if (s != null && !s.equals(""))
                list.add(Integer.valueOf(s));
        }
        return list;
    }

    //从map的params中取一个Integer
    public static Integer getInteger(Map<String, Object> params, String field) {
        String str = (String) params.get(field);
        if (str == null || str.equals(""))
            return null;
        return Integer.valueOf(str);
    }

    //从map的params中取一个String
    public static String getString(Map<String, Object> params, String field) {
        if (!params.containsKey(field)) {
            return null;
        }
        return (String) params.get(field);
    }

    //返沪两个integer之间指定步数的integer集合
    public static List<Integer> getBetweenIntegers(Integer start, Integer end, Integer step) {
        if (start == null || end == null || start > end)
            return List.of();
        List<Integer> list = new LinkedList<>();
        for (int i = start; i <= end; i += step) {
            list.add(i);
        }
        return list;
    }
}
